import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

/**
 * 客人信息面板
 */
public class GuestInfoPanel extends JPanel {
    private JTable guestTable;
    private DefaultTableModel tableModel;
    private JTextField searchField;

    public GuestInfoPanel() {
        setupUI();
        refreshData();
    }

    private void setupUI() {
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);
        setBorder(new EmptyBorder(20, 20, 20, 20));

        // 标题和操作面板
        JPanel topPanel = createTopPanel();
        add(topPanel, BorderLayout.NORTH);

        // 客人信息表格
        JPanel tablePanel = createTablePanel();
        add(tablePanel, BorderLayout.CENTER);
    }

    private JPanel createTopPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.WHITE);
        panel.setBorder(new EmptyBorder(0, 0, 20, 0));

        // 标题
        JLabel titleLabel = new JLabel("客人信息管理");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        titleLabel.setForeground(HotelManagementSystem.getPrimaryColor());
        panel.add(titleLabel, BorderLayout.WEST);

        // 操作按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setBackground(Color.WHITE);

        JButton refreshButton = createStyledButton("刷新", HotelManagementSystem.getSecondaryColor());
        refreshButton.addActionListener(e -> refreshData());
        buttonPanel.add(refreshButton);

        panel.add(buttonPanel, BorderLayout.EAST);

        // 搜索面板
        JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        searchPanel.setBackground(Color.WHITE);

        JLabel searchLabel = new JLabel("搜索客人:");
        searchLabel.setFont(HotelManagementSystem.getButtonFont());
        searchPanel.add(searchLabel);

        searchField = new JTextField(15);
        searchField.setFont(HotelManagementSystem.getButtonFont());
        searchPanel.add(searchField);

        JButton searchButton = createStyledButton("搜索", HotelManagementSystem.getPrimaryColor());
        searchButton.addActionListener(e -> searchGuest());
        searchPanel.add(searchButton);

        panel.add(searchPanel, BorderLayout.SOUTH);

        return panel;
    }

    private JPanel createTablePanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.WHITE);

        // 表格标题
        JLabel tableTitle = new JLabel("客人信息列表");
        tableTitle.setFont(new Font("微软雅黑", Font.BOLD, 16));
        tableTitle.setForeground(HotelManagementSystem.getPrimaryColor());
        tableTitle.setBorder(new EmptyBorder(0, 0, 10, 0));
        panel.add(tableTitle, BorderLayout.NORTH);

        // 创建表格
        String[] columnNames = { "客人ID", "姓名", "身份证号", "电话", "地址" };
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        guestTable = new JTable(tableModel);
        guestTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        guestTable.setRowHeight(30);
        guestTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
        guestTable.getTableHeader().setBackground(HotelManagementSystem.getBackgroundColor());
        guestTable.setSelectionBackground(HotelManagementSystem.getSecondaryColor().brighter());

        JScrollPane scrollPane = new JScrollPane(guestTable);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    private JButton createStyledButton(String text, Color backgroundColor) {
        JButton button = new JButton(text);
        button.setFont(HotelManagementSystem.getButtonFont());
        button.setBackground(backgroundColor);
        button.setForeground(Color.WHITE);
        button.setBorder(new EmptyBorder(8, 15, 8, 15));
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

        // 添加鼠标悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(backgroundColor.darker());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(backgroundColor);
            }
        });

        return button;
    }

    private void searchGuest() {
        String keyword = searchField.getText().trim();
        if (keyword.isEmpty()) {
            refreshData();
            return;
        }

        List<Guest> allGuests = DataManager.getInstance().getAllGuests();
        tableModel.setRowCount(0);

        for (Guest guest : allGuests) {
            if (guest.getName().contains(keyword) ||
                    guest.getGuestId().contains(keyword) ||
                    guest.getIdCard().contains(keyword) ||
                    guest.getPhone().contains(keyword)) {
                Object[] rowData = {
                        guest.getGuestId(),
                        guest.getName(),
                        guest.getIdCard(),
                        guest.getPhone(),
                        guest.getAddress()
                };
                tableModel.addRow(rowData);
            }
        }
    }

    public void refreshData() {
        List<Guest> guests = DataManager.getInstance().getAllGuests();

        // 清空表格
        tableModel.setRowCount(0);

        // 填充表格数据
        for (Guest guest : guests) {
            Object[] rowData = {
                    guest.getGuestId(),
                    guest.getName(),
                    guest.getIdCard(),
                    guest.getPhone(),
                    guest.getAddress()
            };
            tableModel.addRow(rowData);
        }

        // 清空搜索框
        searchField.setText("");
    }
}